<template>
  <div class="mod-menu">
    <div class="mb10">
      <el-button @click="getBack">返回拼团列表</el-button>
    </div>
    <el-form :inline="true" :model="pamars">
      <el-form-item label="商品名称">
        <el-input v-model="pamars.title" placeholder="商品名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="queryGetDataList()">查询</el-button>
        <el-button type="primary" @click="addOrUpdateHandle()">添加</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="dataList" row-key="Id" border style="width: 100%; ">
      <el-table-column prop="title" align="center" label="商品名称">
      </el-table-column>
      <el-table-column prop="min_sale_price" align="center" label="销售价格">
      </el-table-column>
      <el-table-column prop="min_price" align="center" label="活动价格">
      </el-table-column>
      <el-table-column prop="group_people_num" align="center" label="团购规模">
      </el-table-column>
      <el-table-column prop="real_stock_total" align="center" label="可售数量">
      </el-table-column>
      <el-table-column prop="result_sale_num" align="center" label="已售数量">
      </el-table-column>
      <el-table-column prop="sort" align="center" label="商品排序">
      </el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="goOrder(scope.row)"> 团详情 </el-button>
          <el-button type="text" size="small" @click="upGbPdAdd(scope.row.id,scope.row.goods_id)">编辑</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pamars.page" :page-sizes="[10, 20, 50, 100]" :page-size="pamars.limit" :total="totalPage" layout="total, sizes, prev, pager, next, jumper"></el-pagination>
    <seckill-pd-add-update v-if="addOrUpdateVisible" ref="seckillPdAddUpdate" @updataList='getList'></seckill-pd-add-update>
    <!-- 弹窗,修改 -->
    <gb-pd-add v-if="gbPdAddVisible" ref="gbPdAdd" @updataList='getList'></gb-pd-add>
  </div>
</template>

<script>
import seckillPdAddUpdate from './seckill-pd-add-update'
import gbPdAdd from './gb-pd-add'
import seviceApi from '@/api/serviceApi.js'
export default {
  data () {
    return {

      pamars: {
        page: 1,
        limit: 10,
        title: '',
        group_buy_id: ''
      },
      totalPage: 0,
      dataList: [],
      dataListLoading: false,
      addOrUpdateVisible: false,
      gbPdAddVisible: false
    }
  },
  components: {
    seckillPdAddUpdate,
    gbPdAdd
  },
  watch: {
    '$route' () {
      this.getList()
    }
  },
  mounted () {
    this.pamars.group_buy_id = this.$route.query.id
    this.getList()
  },

  methods: {
    queryGetDataList () {
      this.pamars.page = 1
      this.getList()
    },
    goOrder (item) {
      this.$router.push({ name: 'group-order', query: { id: item.group_buy_id, activeId: item.id } })
    },
    deleteHandle (id) {
      this.$confirm(`确定要删除该条数据?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        seviceApi.delGroupbuyActivitygoods(id).then(res => {
          if (res && res.data.code == 200) {
            this.$message({
              message: '删除成功',
              type: 'success'
            })
            this.getList()
          }
        })
      }).catch(() => { })
    },

    // 获取列表
    getList () {
      seviceApi.getGoodsIdList(this.pamars).then(res => {
        if (res && res.data.code == 200) {
          this.dataList = res.data.page.list
          this.totalPage = res.data.page.totalCount
        }
      })
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pamars.limit = val
      this.getList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pamars.page = val
      this.getList()
    },
    // 查询
    getDataList () {
      this.getList()
    },
    // 商品列表
    addOrUpdateHandle (id) {
      this.$router.push({ name: 'gb-addList', query: { id: this.pamars.group_buy_id } })
    },
    // 编辑
    upGbPdAdd (id, goodsId) {
      this.gbPdAddVisible = true
      this.$nextTick(() => {
        this.$refs.gbPdAdd.init(id, this.pamars.group_buy_id, goodsId)
      })
    },
    // 返回拼团列表
    getBack () {
      this.$router.go(-1)
    }
  }
}
</script>
<style>
.mb10 {
  margin-bottom: 10px;
}
</style>
